//
//  ganttJourRes.m
//  testPlanningV2
//
//  Created by richard_local on 17/09/13.
//  Copyright (c) 2013 ierdre.com. All rights reserved.
//

#import "dayDetail.h"

@implementation dayDetail
//@synthesize heureDebut;


-(id) init{
   
    if (self) {
        // Initialization code
        NSDateFormatter *df = [[NSDateFormatter alloc] init];
        [df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_FR"]];

        [df setDateFormat:@"yyyy-MM-dd H:mm:ss"];
        //H pour 24 heure hh pour 12 heure
        DateDebut = [df dateFromString: @"2012-12-25 8:30:00"];
        DateFin =   [df dateFromString: @"2012-12-25 19:00:00"];

        
    
        //Allocation des zones de resultat d'occupation
        allocation = [NSMutableArray arrayWithObjects:@"0", nil];
        //ajout de date pour le test
        // Format  DATE / nbre de personnes / +- vis à vis contraites
       
        [allocation  addObject:DateDebut];
        NSDate *dt = [df dateFromString: @"2012-12-25 10:30:00"];
        [allocation  addObject:dt];

        dt = [df dateFromString: @"2012-12-25 11:00:00"];
        [allocation  addObject:dt];

        dt = [df dateFromString: @"2012-12-25 11:30:00"];
        [allocation  addObject:dt];
        dt = [df dateFromString: @"2012-12-25 15:30:00"];
        [allocation  addObject:dt];
        dt = [df dateFromString: @"2012-12-25 17:30:00"];
        [allocation  addObject:dt];
        [allocation  addObject: DateFin];
        //NSLog(@"%@" , allocation);
         
    }
    return self;

}


-(NSString *) heureDebut{
    NSCalendar* calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    unsigned hourAndMinuteFlags = NSHourCalendarUnit | NSMinuteCalendarUnit;

    NSDateComponents *components = [calendar components:hourAndMinuteFlags fromDate:DateDebut];


    NSString * res = [NSString stringWithFormat:@"%02i:%02i",[components hour],[components minute]];
    return res;
}

-(NSString *) heureFin{
    NSCalendar* calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

    unsigned hourAndMinuteFlags = NSHourCalendarUnit | NSMinuteCalendarUnit;
    NSDateComponents *components = [calendar components:hourAndMinuteFlags fromDate:DateFin];
    NSString * res = [NSString stringWithFormat:@"%02i:%02i",[components hour],[components minute]];
    return res;

}

-(float) precision{
    return 1;
}
/*-(NSMutableArray *) allocation{
    
    
    
    
    
    return res;
}*/
-(int) getEndPositionTransition:(int)num largeurCellule:(int) largeur {
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
   // [calendar setTimeZone:[NSTimeZone timeZoneWithName:@"GMT+1"]];

    NSDateComponents *components = [calendar components:NSHourCalendarUnit | NSMinuteCalendarUnit
                                     fromDate:DateDebut
                                     toDate:DateFin
                                     options:0];
    float dureeJournee= [components hour] + ([components minute] / 60.0);
    
    
    
    // durée de la zone
    /*NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_FR"]];
  
  
    
    [df setDateFormat:@"yyyy-MM-dd H:mm:ss"];*/
    //H pour 24 heure hh pour 12 heure


    
    NSDate * A =  [allocation objectAtIndex:num];
    NSDate * B =  [allocation objectAtIndex:num+1];
    
    components = [calendar components:NSHourCalendarUnit | NSMinuteCalendarUnit
                             fromDate:A
                             toDate:B
                             options:0];

    float dureeIntervalle=  [components hour] + ([components minute] / 60.0);

    return  (int)(dureeIntervalle*largeur/dureeJournee) ;
}
-(int) getEndIndiceTransition:(int)num {
    return (int)(rand()%10 )-5;
}

-(int) nombreDeZone{
    return [allocation count];
}
- (NSString *)description {
    return [NSString stringWithFormat:@"%@ %@",[self heureFin],[self heureDebut]];
}

@end
